package cn.tedu.zblogs.home.service.impl;


import cn.tedu.zblogs.home.mapper.*;
import cn.tedu.zblogs.home.pojo.VO.AdvertisementVO;
import cn.tedu.zblogs.home.pojo.VO.ArticleTagVO;
import cn.tedu.zblogs.home.pojo.VO.ArticleVO;
import cn.tedu.zblogs.home.pojo.VO.TagVO;
import cn.tedu.zblogs.home.pojo.dto.ArticleDTO;
import cn.tedu.zblogs.home.pojo.entity.DataStatistics;
import cn.tedu.zblogs.home.service.HomeService;
import cn.tedu.zblogs.home.utils.CacheUtil;
import cn.tedu.zblogs.home.utils.CaffeineUtil;
import lombok.extern.slf4j.Slf4j;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.swing.text.BadLocationException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class HomeServicelmpl implements HomeService {
    @Autowired
    private AdvertisementMapper advertisementMapper;
    //标签表
    @Autowired
    private TagMapper tagMapper;
    //文章表
    @Autowired
    private ArticleMapper articleMapper;
    //文章标签关系表
    @Autowired
    private ArticleTagMapper articleTagMapper;

    //redis缓存工具类
    @Autowired
    private CacheUtil cacheUtil;

    //内存缓存工具类
    @Autowired
    private CaffeineUtil caffeineUtil;

    @Autowired
    private DataStatisticsMapper dataStatisticsMapper;


    @Override
    public List<AdvertisementVO> selectAll() {
        log.debug("广告查询11");
        //  List<AdvertisementVO> list= advertisementMapper.selectAll1();
        // return list;

        //caffeineUtil  从内存缓存中获取数据
        Serializable caffeineData = caffeineUtil.get("home_advertisement_list_");
        if (caffeineData != null) {
            log.debug("从缓存中获取数据");
            System.out.println("从caffeine内存缓存中获取数据");
            //将缓存数据转化为list
            return (List<AdvertisementVO>) caffeineData;
        }


        //查询redis中获取缓存信息
        Serializable cacheData = cacheUtil.get("home_advertisement_list_");
        if (cacheData != null) {
            log.debug("从缓存中获取数据");
            System.out.println("从redis缓存中获取数据");
            //将缓存数据转化为list
            return (List<AdvertisementVO>) cacheData;
        }

        //查询数据库
        List<AdvertisementVO> list = advertisementMapper.selectAll1();
        System.out.println("从数据库获取数据");



        //将首页列表 缓存 caffeine内存中
        caffeineUtil.set("home_advertisement_list_", (Serializable) list);

        //将首页列表 缓存到redis中
        cacheUtil.setExpire("home_advertisement_list_", (Serializable) list, 60);

          return list;
    }

    @Override
    public List<TagVO> selectHomeTag() {
        log.debug("首页标签查询");

        List<TagVO> list= tagMapper.selectHomeTag();
        //默认添加全部标签
        TagVO list1= new TagVO();
        list1.setId(0);
        list1.setTagName("全部");
        list.add(0,list1);

        //添加标签热门
        TagVO list2= new TagVO();
        list2.setId(-1);
        list2.setTagName("热门");
        list.add(1,list2);

        return list;

    }

    @Override
    public List<ArticleVO> SelectArticle(ArticleDTO articleDTO) {
        System.out.println("查询文章列表");
        log.debug("查询文章列表");



        //caffeineUtil  内存缓存工具类
        Serializable  caffeineData = caffeineUtil.get("home_article_list_" + articleDTO.getId());
        if (caffeineData != null) {
            log.debug("从缓存中获取数据");
            System.out.println("从caffeine内存缓存中获取数据");
            //将缓存数据转化为list
            return (List<ArticleVO>) caffeineData;
        }


        //查询redis缓存
        Serializable cacheData = cacheUtil.get("home_article_list_" + articleDTO.getId());
        if (cacheData != null) {
            log.debug("从缓存中获取数据");
            System.out.println("从redis缓存中获取数据");
            //将缓存数据转化为list
            return (List<ArticleVO>) cacheData;
        }


        List<ArticleVO> list;//定义返回变量
        System.out.println("从数据库获取数据");

        //没有标签id时默认查询全部
        if (articleDTO.getId()==0 || articleDTO == null || articleDTO.getId()==-1){
            //文章列表
             //list= articleMapper.selectArticleAll();
             if (articleDTO.getId()==0 ){//0查全部
                 list=articleMapper.selectArticleAll();
             }else {
                 list=articleMapper.selectArticleTagrm();//-1查热门文章
             }
             for (int i=0;i<list.size();i++){
                 ArticleVO articleVO=list.get(i);
                 //将文章内容缩减为内容的前一部分
                 //获取数据库中的富文本
                 String htmlContent = articleVO.getContent();
                 //解析HTML字符串
                 Document doc = Jsoup.parse(htmlContent);
                 //提取文本内容
                 String textContent = doc.text();
                 if(textContent.length() > 23){
                     //将首页获得的文本内容替换为文本的前20个字
                     articleVO.setContent(textContent.substring(0,23));
                 }else {
                     articleVO.setContent(textContent);
                 }
                 //取出文章id
                 Long articleVOId= articleVO.getId();
                 //文章id 去查询关系表，拿到标签id
                 System.out.println("文章id"+articleVOId);
                 List<ArticleTagVO> list1 = articleTagMapper.ArticleIdSelectTagId(articleVOId);
                 //判断list是否为空
                 if (!list1.isEmpty()){
                     System.out.println(list1);
                     ArticleTagVO  articleTagVO= list1.get(0);
                     int tagId =  articleTagVO.getTagId();
                     //标签id 去查询标签表，拿到标签名

                     List<TagVO> list2= tagMapper.selectTagName(tagId);
                     if (!list2.isEmpty()){//判断list是否为空
                         String tagName= list2.get(0).getTagName();
                         //标签名 赋值给文章对象
                         articleVO.setTagName(tagName);
                     }
                 }
             }
            //将首页列表 缓存 caffeine内存中
            caffeineUtil.set("home_article_list_" + articleDTO.getId(),(Serializable) list);

            //将首页列表 缓存到redis中
            cacheUtil.setExpire("home_article_list_" + articleDTO.getId(),(Serializable) list,60);

          //有标签时查询标签对应的文章
        } else {
             list= articleMapper.selectArticle(articleDTO);

            //将首页列表 缓存 caffeine内存中
            caffeineUtil.set("home_article_list_" + articleDTO.getId(),(Serializable) list);
            //将首页列表 缓存到redis中
            cacheUtil.setExpire("home_article_list_" + articleDTO.getId(),(Serializable) list,60);
        }
        return  list;
    }

    @Override
    public int InsertDataStatistics() {//插入统计 定时任务
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-H");
        String currentDate = dateFormat.format(new Date());//2024-09-26
        currentDate = "PV" + currentDate;//PV2024-09-26
        Integer pv =  cacheUtil.getCount(currentDate);



        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd-H");
        String currentDate1= dateFormat1.format(new Date());//2024-09-26
        currentDate1 = "UV" + currentDate1;//UV2024-09-26
          int uv= cacheUtil.Setsize(currentDate1);

        DataStatistics dataStatistics1=new DataStatistics();
        dataStatistics1.setPV(pv);
        dataStatistics1.setUV( uv);

        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate2= dateFormat2.format(new Date());//2024-09-26
        dataStatistics1.setDate(currentDate2);//2024-09-26

        SimpleDateFormat dateFormat3 = new SimpleDateFormat("H");
        String currentDate3= dateFormat3.format(new Date());//15(小时数)
        dataStatistics1.setHour(currentDate3);
        dataStatistics1.setCreatedAt(new Date());
        dataStatistics1.setUpdatedAt(new Date());

          int pvuv= dataStatisticsMapper.InsertDataStatistics(dataStatistics1);
          return  pvuv;


    }

}
